﻿using Microsoft.AspNetCore.Mvc;
using SlamDunk.MES.IAppService.DTOs.Outputs.ResourceContext;
using SlamDunk.MES.IAppService.Interfaces;
using SlamDunk.MES.ReportView.Models;
using SlamDunk.ValueObjects.Enums;
using System;
using System.Collections.Generic;

namespace SlamDunk.MES.AppService.Host.Controllers
{
    /// <summary>
    /// 报表统计服务WebApi接口
    /// </summary>
    [ApiController]
    [Route("Api/[controller]/[action]")]
    public class ReportController : ControllerBase
    {
        #region # 字段及构造器

        /// <summary>
        /// 报表统计服务契约接口
        /// </summary>
        private readonly IReportContract _reportContract;

        /// <summary>
        /// 依赖注入构造器
        /// </summary>
        public ReportController(IReportContract reportContract)
        {
            this._reportContract = reportContract;
        }

        #endregion

        #region # 获取作业计划物料出/入库概要列表 —— IEnumerable<MaterialSummaryInfo> GetExecutionPlanMaterialBoundSummaries(...
        /// <summary>
        /// 获取作业计划物料出/入库概要列表
        /// </summary>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <param name="directionType">出/入库类型</param>
        /// <returns>物料出/入库概要列表</returns>
        [HttpGet]
        public IEnumerable<MaterialSummaryInfo> GetExecutionPlanMaterialBoundSummaries(Guid executionPlanId, StockDirectionType directionType)
        {
            return this._reportContract.GetExecutionPlanMaterialBoundSummaries(executionPlanId, directionType);
        }
        #endregion

        #region # 获取作业计划人员使用概要列表 —— IEnumerable<PersonSummaryInfo> GetExecutionPlanPersonUseSummaries(...
        /// <summary>
        /// 获取作业计划人员使用概要列表
        /// </summary>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <returns>人员使用概要列表</returns>
        [HttpGet]
        public IEnumerable<PersonSummaryInfo> GetExecutionPlanPersonUseSummaries(Guid executionPlanId)
        {
            return this._reportContract.GetExecutionPlanPersonUseSummaries(executionPlanId);
        }
        #endregion

        #region # 获取作业计划设备使用概要列表 —— IEnumerable<EquipmentSummaryInfo> GetExecutionPlanEquipmentUseSummaries(...
        /// <summary>
        /// 获取作业计划设备使用概要列表
        /// </summary>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <returns>设备使用概要列表</returns>
        [HttpGet]
        public IEnumerable<EquipmentSummaryInfo> GetExecutionPlanEquipmentUseSummaries(Guid executionPlanId)
        {
            return this._reportContract.GetExecutionPlanEquipmentUseSummaries(executionPlanId);
        }
        #endregion

        #region # 获取作业计划物料使用概要列表 —— IEnumerable<MaterialSummaryInfo> GetExecutionPlanMaterialUseSummaries(...
        /// <summary>
        /// 获取作业计划物料使用概要列表
        /// </summary>
        /// <param name="executionPlanId">作业计划Id</param>
        /// <param name="directionType">物料输入/输出类型</param>
        /// <returns>物料使用概要列表</returns>
        [HttpGet]
        public IEnumerable<MaterialSummaryInfo> GetExecutionPlanMaterialUseSummaries(Guid executionPlanId, MaterialDirectionType directionType)
        {
            return this._reportContract.GetExecutionPlanMaterialUseSummaries(executionPlanId, directionType);
        }
        #endregion

        #region # 获取产品合格率字典 —— IEnumerable<KeyValuePair<MaterialInfo, decimal>> GetProductQualifiedRates(...
        /// <summary>
        /// 获取产品合格率字典
        /// </summary>
        /// <param name="inspectedDate">质检日期</param>
        /// <returns>产品合格率字典</returns>
        [HttpGet]
        public IEnumerable<KeyValuePair<MaterialInfo, decimal>> GetProductQualifiedRates(DateTime inspectedDate)
        {
            IDictionary<MaterialInfo, decimal> dictionary = this._reportContract.GetProductQualifiedRates(inspectedDate);

            IList<KeyValuePair<MaterialInfo, decimal>> keyValuePairs = new List<KeyValuePair<MaterialInfo, decimal>>();
            foreach (KeyValuePair<MaterialInfo, decimal> kv in dictionary)
            {
                keyValuePairs.Add(new KeyValuePair<MaterialInfo, decimal>(kv.Key, kv.Value));
            }

            return keyValuePairs;
        }
        #endregion

        #region # 获取物料投入周期概要字典 —— {DateTime, MaterialQuantityInfo[]} GetMaterialInputPeriodicSummaries(...
        /// <summary>
        /// 获取物料投入周期概要字典
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>物料投入周期概要字典</returns>
        /// <remarks>
        /// 键：日期，值：物料数量列表
        /// </remarks>
        [HttpGet]
        public IDictionary<DateTime, MaterialQuantityInfo[]> GetMaterialInputPeriodicSummaries(DateTime startTime, DateTime endTime)
        {
            return this._reportContract.GetMaterialInputPeriodicSummaries(startTime, endTime);
        }
        #endregion

        #region # 获取产品产出周期概要字典 —— {DateTime, ProductQuantityInfo[]} GetProductOutputPeriodicSummaries(...
        /// <summary>
        /// 获取产品产出周期概要字典
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>产品产出周期概要字典</returns>
        /// <remarks>
        /// 键：日期，值：产品数量列表
        /// </remarks>
        [HttpGet]
        public IDictionary<DateTime, ProductQuantityInfo[]> GetProductOutputPeriodicSummaries(DateTime startTime, DateTime endTime)
        {
            return this._reportContract.GetProductOutputPeriodicSummaries(startTime, endTime);
        }
        #endregion

        #region # 获取作业计划状态/数量字典 —— IDictionary<ExecutionPlanStatus, int> GetExecutionPlanStatusCounts(...
        /// <summary>
        /// 获取作业计划状态/数量字典
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>作业计划状态/数量字典</returns>
        [HttpGet]
        public IEnumerable<KeyValuePair<ExecutionPlanStatus, int>> GetExecutionPlanStatusCounts(DateTime startTime, DateTime endTime)
        {
            IDictionary<ExecutionPlanStatus, int> dictionary = this._reportContract.GetExecutionPlanStatusCounts(startTime, endTime);

            IList<KeyValuePair<ExecutionPlanStatus, int>> keyValuePairs = new List<KeyValuePair<ExecutionPlanStatus, int>>();
            foreach (KeyValuePair<ExecutionPlanStatus, int> kv in dictionary)
            {
                keyValuePairs.Add(new KeyValuePair<ExecutionPlanStatus, int>(kv.Key, kv.Value));
            }

            return keyValuePairs;
        }
        #endregion

        #region # 获取产品数量列表 —— IEnumerable<ProductQuantityInfo> GetProductQuantities(DateTime startTime...
        /// <summary>
        /// 获取产品数量列表
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns>产品数量列表</returns>
        [HttpGet]
        public IEnumerable<ProductQuantityInfo> GetProductQuantities(DateTime startTime, DateTime endTime)
        {
            return this._reportContract.GetProductQuantities(startTime, endTime);
        }
        #endregion
    }
}
